package com.czhlearn.controller;

import com.czhlearn.pojo.Article;
import com.czhlearn.pojo.PageBean;
import com.czhlearn.pojo.Result;
import com.czhlearn.service.ArticleService;
import com.czhlearn.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 文章
 *
 * @program: big-event
 * @description:
 * @author: czh
 * @create: 2023-12-18 09:20
 **/

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 添加文章
     */

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     * 文章列表查询
     *
     * @param pageNum    页数
     * @param pageSize   每页条数
     * @param categoryId 分类ID
     * @param state      状态
     * @return
     */

    @GetMapping
    public Result<PageBean<Article>> getArticleList(Integer pageNum, Integer pageSize,
            /*@RequestParam */String categoryId,
            /*@RequestParam*/ String state) {

        PageBean<Article> pageBean = articleService.getArticleList(pageNum, pageSize, categoryId, state);

        return Result.success(pageBean);

    }


    /**
     * 更新文章内容
     *
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Article article) {
        articleService.update(article);
        return Result.success();
    }

    /**
     * 文章详情
     *
     * @param id 文章id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article article = articleService.detail(id);
        return Result.success(article);
    }

    /**
     * 删除文章
     * @param id 文章ID
     * @return
     */


    @DeleteMapping
    public Result delete(Integer id) {
        articleService.delete(id);
        return Result.success();
    }


}
